<?php

namespace App\Http\Controllers\Api\Admin;

use App\Http\Controllers\Api\Controller;
use App\Models\Student;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class StudentController extends Controller
{

  //列表
  public function index(Request $request)
  {
    $cates = Student::getList($request);
    return $this->success($cates);
  }

  // 详情
  public function detail($id)
  {
    $cateInfo = Student::getInfo($id);
    return $this->success($cateInfo);
  }

  // 修改
  public function update(Request $request)
  {
    $cateInfo = DB::table('students')->where('id', $request->id)->first();
    if ($cateInfo && $cateInfo->id) {
      $updateData = [
        'is_read' => $request->is_read ?? '',
        'remark' => $request->remark ?? '',
        'updated_at' => date('Y-m-d H:i:s'),
      ];
      $result = DB::table('students')->where('id', $request->id)->update($updateData);
      if ($result) {
        return $this->success();
      }
      return $this->failed('修改失败');
    } else {
      return $this->failed('不存在');
    }
  }

  // 删除
  public function delete($id)
  {
    $result = Student::del($id);
    if ($result) {
      return $this->success();
    }
    return $this->failed('删除失败');
  }
}
